import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

class Student {
    public int studentId;
    public int studentClass;
}

@WebServlet("/studentInfo")
public class StudentInfoServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 假设客户端发送来的请求是形如 /studentInfo?studentId=24&studentClass=6
        // 那就可以通过 getParameter 来获得两个 id 的值
        String queryString = req.getQueryString();
        System.out.println(queryString);

        String studentClass = req.getParameter("studentClass");
        String studentId = req.getParameter("studentId");

        resp.getWriter().write("studentClass = " + studentClass + " " + "studentId = " + studentId);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        // 假设通过 body 传输
//        // 并且约定以 application/x-www-form-urlencoded 的格式传输数据
//        String queryString = req.getQueryString();
//        System.out.println(queryString);
//
//        String studentClass = req.getParameter("studentClass");
//        String studentId = req.getParameter("studentId");
//
//        resp.getWriter().write("studentClass = " + studentClass + " " + "studentId = " + studentId);

        // 从这里开始处理 JackSon 的请求
        // 从请求的 body 中开始读取, 并且解析
        ObjectMapper objectMapper = new ObjectMapper();
        Student student = objectMapper.readValue(req.getInputStream(), Student.class);
    }
}
